package leetcode.秋招.腾讯;

import leetcode.剑指offer.pojo.ListNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author ：zsy
 * @date ：Created 2021/9/5 20:02
 * @description：
 */
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param lists ListNode类一维数组 指向这些数链的开头
     * @return ListNode类
     */
    public ListNode solve (ListNode[] lists) {
        // write code here
        if (lists.length == 0) return null;
        ListNode head = new ListNode(-1);
        ListNode temp = head;
        Queue<ListNode> queue = new LinkedList<>();
        for (int i = 0; i < lists.length; i++) {
            if (lists[i] != null)
                queue.offer(lists[i]);
        }
        while (!queue.isEmpty()) {
            int size = queue.size();
            if (size == 1) {
                temp.next = queue.poll();
                break;
            }
            for (int i = 0; i < size; i++) {
                ListNode cur = queue.poll();
                temp.next = cur;
                cur = cur.next;
                if (cur != null) {
                    queue.offer(cur);
                }
                temp = temp.next;
            }
        }
        return head.next;
    }
}
